package com.lin.activity;


import com.lin.utils.DatabaseHelperForChattingMap;
import com.yunfeng.supercall.R;

import android.app.Activity;  
import android.content.ContentValues;  
import android.database.Cursor;  
import android.database.sqlite.SQLiteDatabase;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
  
public class STestDatabaseActivity extends Activity implements OnClickListener{  
    private final static String SWORD="SWORD";  
    //声明五个控件对象  
    Button createDatabase=null;  
    Button updateDatabase=null;  
    Button insert=null;  
    Button update=null;  
    Button query=null;  
    Button delete=null;  
      
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main_database);  
        findViews();  
    }  
  
    private void findViews() {  
        //根据控件ID得到控件  
        createDatabase = (Button) this.findViewById(R.id.createDatabase);  
        updateDatabase = (Button) this.findViewById(R.id.updateDatabase);  
        insert = (Button) this.findViewById(R.id.insert);  
        update = (Button) this.findViewById(R.id.update);  
        query = (Button) this.findViewById(R.id.query);  
        delete = (Button) this.findViewById(R.id.delete);  
        //添加监听器  
        createDatabase.setOnClickListener(this);  
        updateDatabase.setOnClickListener(this);  
        insert.setOnClickListener(this);  
        update.setOnClickListener(this);  
        query.setOnClickListener(this);  
        delete.setOnClickListener(this);  
    }  
  
    @Override  
    public void onClick(View v) {  
        //判断所触发的被监听控件，并执行命令  
        switch(v.getId()){  
        //创建数据库  
        case R.id.createDatabase:  
            //创建一个DatabaseHelper对象  
			DatabaseHelperForChattingMap dbHelper1 = new DatabaseHelperForChattingMap(STestDatabaseActivity.this, "db3");  
            //取得一个只读的数据库对象  
            SQLiteDatabase db1 = dbHelper1.getReadableDatabase();  
            break;  
        //更新数据库  
        case R.id.updateDatabase:  
            DatabaseHelperForChattingMap dbHelper2 = new DatabaseHelperForChattingMap(STestDatabaseActivity.this, "db3", 2);  
            SQLiteDatabase db2 = dbHelper2.getReadableDatabase();  
            break;  
        //插入数据  
        case R.id.insert:  
            //创建存放数据的ContentValues对象
        	

            ContentValues values = new ContentValues();  
            //像ContentValues中存放数据  
            values.put("id", "id");  
            DatabaseHelperForChattingMap dbHelper3 = new DatabaseHelperForChattingMap(STestDatabaseActivity.this, "UCSMessage3", 2);  
            SQLiteDatabase db3 = dbHelper3.getWritableDatabase();  
            //数据库执行插入命令  
            long i = db3.insert("UCSMessage3", null, values);
            Log.i(SWORD,"insert");  
            break;  
        //更新数据信息  
        case R.id.update:  
            DatabaseHelperForChattingMap dbHelper4 = new DatabaseHelperForChattingMap(STestDatabaseActivity.this, "db3", 2);  
            SQLiteDatabase db4 = dbHelper4.getWritableDatabase();  
            ContentValues values2 = new ContentValues();  
            values2.put("name", "xiaosan");  
            int i2 = db4.update("UCSMessage3", values2, "id=?", new String[]{"1"});  
            break;  
        //查询信息  
        case R.id.query: 
        	
        	
            DatabaseHelperForChattingMap dbHelper5 = new DatabaseHelperForChattingMap(STestDatabaseActivity.this, "db3", 2);  
            SQLiteDatabase db5 = dbHelper5.getReadableDatabase();
            
/*            values.put("id", "id");  
            values.put("chatType","chatType");
            values.put("content","content");
            values.put("msgFrom","msgFrom");
            values.put("msgId","msgId");
            values.put("msgTo","msgTo");
            values.put("receiveTime","receiveTime");
            values.put("voiceLength","voiceLength");*/
            //创建游标对象  
            
            Cursor cursor = db5.query("UCSMessage3", new String[]{"id"}, null, null, null, null, null, null);  
            //利用游标遍历所有数据对象  
            while(cursor.moveToNext()){  
            	Log.i(SWORD,"query-->");  
                int idr = cursor.getInt(cursor.getColumnIndex("idr"));
                String id = cursor.getString(cursor.getColumnIndex("id"));
                String username = cursor.getString(cursor.getColumnIndex("chatType"));
                String unreadCount =  cursor.getString(cursor.getColumnIndex("content"));
                String msgTo =  cursor.getString(cursor.getColumnIndex("msgFrom"));
                String msgFrom =  cursor.getString(cursor.getColumnIndex("msgId"));
                String unread =  cursor.getString(cursor.getColumnIndex("msgTo"));
                String receiveTime =  cursor.getString(cursor.getColumnIndex("receiveTime"));
                String voiceLength =  cursor.getString(cursor.getColumnIndex("voiceLength"));
                //日志打印输出  
            }  
            break;  
        //删除记录  
        case R.id.delete:  
            DatabaseHelperForChattingMap dbHelper6 = new DatabaseHelperForChattingMap(STestDatabaseActivity.this,"db3", 2);  
            SQLiteDatabase db6 = dbHelper6.getWritableDatabase();  
            db6.delete("UCSMessage3", "id=?", new String[]{"1"});  
            break;  
        default:  
            Log.i(SWORD,"error");  
            break;  
        }  
    }  
}  